package org.yy;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import cn.hutool.core.util.StrUtil;
import swing2swt.layout.BorderLayout;

public class App {

	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;

	/**
	 * Launch the application.
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			App window = new App();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		centerInDisplay(shell);//显示屏幕中央
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(679, 447);
		shell.setText("SWT Application");
		shell.setLayout(new BorderLayout(0, 0));

		TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
		tabFolder.setLayoutData(BorderLayout.CENTER);

		TabItem tbtmUrldecoder = new TabItem(tabFolder, SWT.NONE);
		tbtmUrldecoder.setText("URLDecoder");

		Composite composite = new Composite(tabFolder, SWT.NONE);
		tbtmUrldecoder.setControl(composite);

		composite.setLayout(new BorderLayout(0, 0));

		ToolBar toolBar = new ToolBar(composite, SWT.RIGHT);
		toolBar.setLayoutData(BorderLayout.NORTH);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.setText(" URL解码 ");
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String url = text.getText();
				try {
					url = URLDecoder.decode(url, "UTF-8");
//					int i = url.indexOf("?");
//					String substring = url.substring(i + 1);
//					String[] split = substring.split("&");
//					String join = String.join("\n", split);
//					url = url + "\n\n" + join;
				} catch (UnsupportedEncodingException exception) {
					exception.printStackTrace();
				}
				text_1.setText(url);
			}
		});

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.setText(" URL编码 ");
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String url = text.getText();
				try {
					url = URLEncoder.encode(url, "UTF-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				text_1.setText(url);
			}
		});

		// 新增按钮：提取URL参数并换行显示（包含基础URL）
		ToolItem tltmExtractParams = new ToolItem(toolBar, SWT.NONE);
		tltmExtractParams.setText("提取参数");
		tltmExtractParams.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String url = text.getText();
				if (url != null && url.contains("?")) {
					// 分割基础URL和查询参数
					String baseUrl = url.substring(0, url.indexOf("?"));
					String queryString = url.substring(url.indexOf("?") + 1);

					// 处理查询参数
					String[] params = queryString.split("&");
					String paramsFormatted = String.join("\n", params);

					// 组合最终输出
					String result = "基础URL:\n" + baseUrl + "\n\n查询参数:\n" + paramsFormatted;
					text_1.setText(result);
				} else {
					text_1.setText("URL中没有查询参数\n完整URL:\n" + url);
				}
			}
		});

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.setText(" 全大写 ");
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_1.setText(text.getText().toUpperCase());
			}
		});

		ToolItem tltmNewItem_3 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_3.setText(" 全小写 ");
		tltmNewItem_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_1.setText(text.getText().toLowerCase());
			}
		});

		ToolItem tltmNewItem_4 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_4.setText("驼峰 --> 蛇形");
		tltmNewItem_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String text2 = text.getText();
				String underlineCase = StrUtil.toUnderlineCase(text2);
				text_1.setText(underlineCase);
			}
		});

		ToolItem tltmNewItem_5 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_5.setText("蛇形 --> 驼峰");
		tltmNewItem_5.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String text2 = text.getText();
				String camelCase = StrUtil.toCamelCase(text2);
				text_1.setText(camelCase);
			}
		});

		ToolItem tltmNewItem_12 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_12.setText(" 复 制 ");
		tltmNewItem_12.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_1.selectAll();
				text_1.copy();
			}
		});

		ToolItem tltmNewItem_9 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_9.setText(" 粘 贴 ");
		tltmNewItem_9.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText("");
				text.paste();
				text.setSelection(0);
			}
		});

		ToolItem tltmNewItem_10 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_10.setText(" 清 空 ");
		tltmNewItem_10.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText("");
			}
		});

		SashForm sashForm = new SashForm(composite, SWT.SMOOTH | SWT.VERTICAL);
		sashForm.setSashWidth(8);
		sashForm.setLayoutData(BorderLayout.CENTER);

		text = new Text(sashForm, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
		text.setFont(SWTResourceManager.getFont("Courier New", 16, SWT.BOLD));

		text_1 = new Text(sashForm, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
		text_1.setFont(SWTResourceManager.getFont("Courier New", 16, SWT.BOLD));
		sashForm.setWeights(new int[] { 1, 1 });

		TabItem tbtmJsonViewer = new TabItem(tabFolder, SWT.NONE);
		tbtmJsonViewer.setText("JSON Viewer");

		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tbtmJsonViewer.setControl(composite_1);
		composite_1.setLayout(new BorderLayout(0, 0));

		TabFolder tabFolder_1 = new TabFolder(composite_1, SWT.NONE);
		tabFolder_1.setLayoutData(BorderLayout.CENTER);

		TabItem tbtmTxt = new TabItem(tabFolder_1, SWT.NONE);
		tbtmTxt.setText("Text ");

		Composite composite_3 = new Composite(tabFolder_1, SWT.NONE);
		tbtmTxt.setControl(composite_3);
		composite_3.setLayout(new BorderLayout(0, 0));

		ToolBar toolBar_1 = new ToolBar(composite_3, SWT.FLAT | SWT.RIGHT);
		toolBar_1.setLayoutData(BorderLayout.NORTH);

		ToolItem tltmNewItem_6 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_6.setText("粘  贴");
		tltmNewItem_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_2.setText("");
				text_2.paste();
				text_2.setSelection(0);
			}
		});

		ToolItem tltmNewItem_7 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_7.setText("复  制");
		tltmNewItem_7.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_2.selectAll();
				text_2.copy();
			}
		});

		ToolItem tltmNewItem_8 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_8.setText("格式化");
		tltmNewItem_8.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String json = text_2.getText();
				if (json == null || "".equals(json)) {
					text_2.setText("");
				} else {
					try {
						//FASTJSON
						//@SuppressWarnings("unchecked")
						//LinkedHashMap<String, Object> jsonObject = JSON.parseObject(json, LinkedHashMap.class, Feature.OrderedField);
						//String pretty = JSONObject.toJSONString(jsonObject, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
						//GSSON
						Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
						JsonParser jp = new JsonParser();
						JsonElement je = jp.parse(json);
						String pretty = gson.toJson(je);

						text_2.setText(pretty);
					} catch (Exception e2) {
						System.out.println("格式化json字符串异常");
					}

				}

			}
		});

		ToolItem tltmNewItem_11 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_11.setText(" 清 空 ");
		tltmNewItem_11.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_2.setText("");
			}
		});

		text_2 = new Text(composite_3, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
		text_2.setTabs(4);
		text_2.setFont(SWTResourceManager.getFont("Courier New", 16, SWT.BOLD));
		text_2.setLayoutData(BorderLayout.CENTER);

		TabItem tbtmViewer = new TabItem(tabFolder_1, SWT.NONE);
		tbtmViewer.setText("Viewer");

		Composite composite_4 = new Composite(tabFolder_1, SWT.NONE);
		tbtmViewer.setControl(composite_4);

		TabItem tabItem_2 = new TabItem(tabFolder, SWT.NONE);
		tabItem_2.setText("New Item33");

		Composite composite_2 = new Composite(tabFolder, SWT.NONE);
		tabItem_2.setControl(composite_2);

	}

	private static void centerInDisplay(Shell shell) {
		//        Rectangle displayArea = shell.getDisplay().getClientArea();
		//        shell.setBounds(displayArea.width / 4, displayArea.height / 4, displayArea.width / 2, displayArea.height / 2);

		Rectangle displayArea = shell.getDisplay().getClientArea();
		int width = displayArea.width;
		int height = displayArea.height;

		Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //取得当前屏幕属性
		height = (int) d.getHeight();
		width = (int) d.getWidth();

		shell.setBounds(width / 4, height / 4, (int) (width * 0.7), (int) (height * 0.7));
		int w = (int) (width * 0.7);
		int h = (int) (height * 0.7);
		shell.setLocation((width - w) / 2, (height - h) / 2);

	}
}
